package cn.iocoder.yudao.module.biz.service.specialWork;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.specialWork.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.specialWork.BizSpecialWorkDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.specialWork.BizSpecialWorkConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.specialWork.BizSpecialWorkMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 工种 Service 实现类
 *
 * @author guangpu
 */
@Service
@Validated
public class BizSpecialWorkServiceImpl implements BizSpecialWorkService {

    @Resource
    private BizSpecialWorkMapper specialWorkMapper;

    @Override
    public String createSpecialWork(BizSpecialWorkCreateReqVO createReqVO) {
        // 插入
        BizSpecialWorkDO specialWork = BizSpecialWorkConvert.INSTANCE.convert(createReqVO);
        specialWorkMapper.insert(specialWork);
        // 返回
        return specialWork.getId();
    }

    @Override
    public void updateSpecialWork(BizSpecialWorkUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSpecialWorkExists(updateReqVO.getId());
        // 更新
        BizSpecialWorkDO updateObj = BizSpecialWorkConvert.INSTANCE.convert(updateReqVO);
        specialWorkMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpecialWork(String id) {
        // 校验存在
        this.validateSpecialWorkExists(id);
        // 删除
        specialWorkMapper.deleteById(id);
    }

    private void validateSpecialWorkExists(String id) {
        if (specialWorkMapper.selectById(id) == null) {
            throw exception(SPECIAL_WORK_NOT_EXISTS);
        }
    }

    @Override
    public BizSpecialWorkDO getSpecialWork(String id) {
        return specialWorkMapper.selectById(id);
    }

    @Override
    public List<BizSpecialWorkDO> getSpecialWorkList(Collection<String> ids) {
        return specialWorkMapper.selectBatchIds(ids);
    }

    @Override
    public List<BizSpecialWorkDO> getSpecialWorkListByTypeId(String typeId) {
        return specialWorkMapper.selectList("work_type_id", typeId);
    }

    @Override
    public PageResult<BizSpecialWorkDO> getSpecialWorkPage(BizSpecialWorkPageReqVO pageReqVO) {
        return specialWorkMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizSpecialWorkDO> getSpecialWorkList(BizSpecialWorkExportReqVO exportReqVO) {
        return specialWorkMapper.selectList(exportReqVO);
    }

}
